#include "mycoin.h"
#include <QPixmap>
#include <QPixmap>
#include <QDebug>
#include <QTimer>
//MyCoin::MyCoin(QWidget *parent) : QPushButton(parent)
//{

//}

MyCoin::MyCoin(QString btnImg)
{
    QPixmap pix;
    bool ret=pix.load(btnImg);
    if(!ret)
    {
        qDebug()<<"图片加载失败";
        return;
    }
    //设置固定大小
    this->setFixedSize(pix.width(),pix.height());
    //设置风格样式
    this->setStyleSheet("QPushButton{border:0px;}");
    //设置图标
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));
    //初始化定时器
    timer1=new QTimer(this);
    timer2=new QTimer(this);

    //监听1->0
    connect(timer1,&QTimer::timeout,this,[=]()
    {
       QPixmap pix;
       QString str=QString(":/resource/Coin000%1.png").arg(this->min++);
       pix.load(str);

       //设置固定大小
       this->setFixedSize(pix.width(),pix.height());
       //设置风格样式
       this->setStyleSheet("QPushButton{border:0px;}");
       //设置图标
       this->setIcon(pix);
       this->setIconSize(QSize(pix.width(),pix.height()));
       //判断是否结束
       if(this->min>this->max)
       {
           this->min=1;
           isAnimation=false;
           timer1->stop();
       }
    });

    //监听0->1
    connect(timer2,&QTimer::timeout,this,[=]()
    {
       QPixmap pix;
       QString str=QString(":/resource/Coin000%1.png").arg(this->max--);
       pix.load(str);

       //设置固定大小
       this->setFixedSize(pix.width(),pix.height());
       //设置风格样式
       this->setStyleSheet("QPushButton{border:0px;}");
       //设置图标
       this->setIcon(pix);
       this->setIconSize(QSize(pix.width(),pix.height()));
       //判断是否结束
       if(this->max<this->min)
       {
           this->max=8;
           isAnimation=false;
           timer2->stop();
       }
    });
}

void MyCoin::changeFlag()
{
    //如果正面，翻成反面
    if(this->flag)
    {
        timer1->start(30);
        isAnimation=true;//开始动画
        this->flag=false;

    }
    else
    {
        timer2->start(30);
        isAnimation=true;
        this->flag=true;
    }
}

void MyCoin::mousePressEvent(QMouseEvent *e)
{
    if(this->isAnimation||this->isWin)
    {
        return;
    }
    else
    {
        QPushButton::mousePressEvent(e);
    }
}
